//
// Created by 24137 on 2021/7/5.
//

#ifndef DATA_STRUCTURE_ARRAY_H
#define DATA_STRUCTURE_ARRAY_H

#endif //DATA_STRUCTURE_ARRAY_H

//---------------数组的顺序存储表示-------------

#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>

#define element int

#define MAX_ARRAY_DIM 8//数组最大维度8

typedef struct
{
    element *base;//基址地址
    int dim;//维度
    int *bounds; //数组维界基址
    int *constants;//数组映像函数常量基址
} Array;

bool initArray(Array *A, int dim, ...)
{
    if (dim < 1 || dim > MAX_ARRAY_DIM)return false;
    A->dim = dim;
    A->bounds = (int *) malloc(sizeof(int));
    if (!A->bounds)return false;
    int elemtotal = 1;
    va_list ap;
    va_start(ap, dim);
    for (int i = 0; i < dim; ++i)
    {
        A->bounds[i] = va_arg(ap, int);
        elemtotal * = A->bounds[i];
    }
    va_end(ap);

    A->base = (int *) malloc(sizeof(int) * elemtotal);
    A->constants = (int *) malloc(dim * sizeof(int));
    A->constants[dim - 1] = 1;  //最后的一维是一维数组，其参数固定为1

    for (int i = dim - 2; i >= 0; --i)
    {
        A->constants[i] = A->bound[i + 1] * A->constants[i + 1];
    }
}
